package com.alibaba.dongbao.utils.advice;

import com.alibaba.dongbao.base.result.Result;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.baomidou.kaptcha.exception.KaptchaException;
import com.baomidou.kaptcha.exception.KaptchaIncorrectException;
import com.baomidou.kaptcha.exception.KaptchaNotFoundException;
import com.baomidou.kaptcha.exception.KaptchaTimeoutException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author: Mr.ZJW
 * @Date: 2022-04-11 9:53
 * @Description: 异常统一处理器
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * @Author: Mr.ZJW
     * @Description: Token登录异常
     * @Date: 2022/4/11 9:54
     **/
    @ExceptionHandler(TokenExpiredException.class)
    public Result loginException(Exception e) {
        return Result.error(e.getMessage());
    }

    /**
     * @param [kaptchaException]
     * @Author: Mr.ZJW
     * @Description: Kaptcha验证码异常处理
     * @Date: 2022/4/25 15:58
     **/
    @ExceptionHandler(value = KaptchaException.class)
    public Result kaptchaExceptionHandler(KaptchaException kaptchaException) {
        if (kaptchaException instanceof KaptchaIncorrectException) {
            return Result.error("验证码不正确");
        } else if (kaptchaException instanceof KaptchaNotFoundException) {
            return Result.error("验证码未找到");
        } else if (kaptchaException instanceof KaptchaTimeoutException) {
            return Result.error("验证码过期");
        } else {
            return Result.error("验证码渲染失败");
        }
    }

}
